﻿package pages
{
	import com.gaiaframework.templates.AbstractPage;
	import com.gaiaframework.events.*;
	import com.gaiaframework.debug.*;
	import com.gaiaframework.api.*;
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	
	import com.greensock.TweenMax;
	import com.video.VideoPlayerEvent;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.text.TextLineMetrics;
	import model.Model;
	import ui.videos.CustomSlider;
	import utils.ShapeUtil;
	import utils.TextFieldUtil;
	
	public class PiPage extends AbstractPage
	{	
		
		private var _title:String, _subtitle:String,_timelineTitle:String,_timelineSubTitle:String,_description:String
		private var _slide:CustomSlider;
		private var _containerTitle:Sprite;
		private var _colorTitle:Number;
		private var _tracker:AnalyticsTracker;
		public function PiPage()
		{
			super();
			alpha = 0;
			//new Scaffold(this);
		}
		override public function transitionIn():void 
		{
			
			super.transitionIn();
			_tracker = new GATracker(this, Model.instance.getGa(), "AS3", false);
			_tracker.trackPageview(this.page.id);
			TextFieldUtil.setStyle(IStyleSheet(Gaia.api.getPage(Pages.INDEX).assets.styles).style);
			Model.instance.uiData = IXml(Gaia.api.getPage(Pages.INDEX).assets.uiData).xml;
			_title =  Model.instance.getUiLabel(this.page.id + '-title');
			_subtitle =  Model.instance.getUiLabel(this.page.id + '-subtitle');
			_timelineTitle =  Model.instance.getUiLabel(this.page.id + '-timelineTitle');
			_timelineSubTitle =  Model.instance.getUiLabel(this.page.id + '-timelineSubTitle');
			_description =  Model.instance.getUiLabel(this.page.id + '-description');
			_colorTitle = Number(Model.instance.getUiLabel(this.page.id + '-colorTitle'));
			if (this.assets)
			{
				_slide = new CustomSlider(this.page.assetArray);
				_slide.addEventListener(VideoPlayerEvent.VIDEO_READY, _videoReady);
				_slide.addEventListener(VideoPlayerEvent.VIDEO_COMPLETE, _videoComplete);
				addChild(_slide);
			}
			addTitleAndSubTitle();
			_onResize();
			stage.addEventListener(Event.RESIZE, _onResize);
			TweenMax.to(this, 0.5, {alpha:1, onComplete:transitionInComplete } );
		}
		
		private function _videoComplete(e:VideoPlayerEvent):void 
		{
			if (_containerTitle)
			{
				TweenMax.to(_containerTitle, 2, { alpha:1,onComplete:doNext } );
			}
		}
		
		private function doNext():void 
		{
			var next:String = this.page.node.@next
			if (next != " ")
			{
				Gaia.api.goto(Pages.TIMELINE + '/' + next);
			}
		}
		
		
		private function _videoReady(e:VideoPlayerEvent):void 
		{
			if (_containerTitle)
			{
				TweenMax.to(_containerTitle, 2, { alpha:0 } );
			}
		}
		
		private function _onResize(e:Event=null):void 
		{
			
		}
		
		private function addTitleAndSubTitle():void 
		{
			_containerTitle = new Sprite();
			addChild(_containerTitle);
			_containerTitle.mouseEnabled = false;
			var title:TextField = new TextField();
			//title.width = 500;
			title.selectable = false;
			title.wordWrap = true;
			title.multiline = true;
			title.width = 500;
			title.x = 10;
			TextFieldUtil.setText(title, "<titlePi>" + _title + "</titlePi>");
			TweenMax.to(title, 0, { tint:_colorTitle } );
			TweenMax.to(_containerTitle, 0, {dropShadowFilter:{color:0x000000, alpha:1, blurX:12, blurY:12, distance:12}});
			_containerTitle.addChild(title);
			var metrics:TextLineMetrics = title.getLineMetrics(0);
			//var sh:Shape = ShapeUtil.getLineV(100, 0xffffff);
			var sh:Sprite = ShapeUtil.getSprite(2,title.height-55,0xffffff);
			
			
			sh.y =  title.y + 28;
			sh.x =  (title.x + title.width) + 5;
			_containerTitle.addChild(sh);
			
			
			var subTitle:TextField = new TextField();
			subTitle.selectable = false;
			subTitle.wordWrap = true
			subTitle.multiline = true;
			subTitle.width = 400;
			TextFieldUtil.setText(subTitle, "<subTitlePi>" + _subtitle + "</subTitlePi>",true);
			_containerTitle.addChild(subTitle);
			
			subTitle.y = (title.y + title.height) - (subTitle.height+20);
			subTitle.x = sh.x +sh.width + 31;
			//addBackgroundColor(_containerTitle, title);	
		}
		
		private function addBackgroundColor(containerTitle:Sprite, title:TextField):void 
		{
			var len:int = title.numLines;
			for(var i:int = 0; i < len; i++) {
				_containerTitle.graphics.beginFill(0xFFFFFFF, 1);
				var metrics:TextLineMetrics = title.getLineMetrics(i);
				_containerTitle.graphics.drawRect(title.x + metrics.x, title.y + i * metrics.height, metrics.width, metrics.height + metrics.descent);
				_containerTitle.graphics.endFill();
			}
		}
		override public function transitionOut():void 
		{
			super.transitionOut();
			stage.removeEventListener(Event.RESIZE, _onResize);
			TweenMax.to(this, 0.3, { alpha:0, onComplete:function():void { if (_slide)_slide.destroy(); removeChild(_containerTitle); transitionOutComplete() }} );

		}
	}
}